package com.xwolf.os.message;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.*;

/**
 * @author ming
 * @Description:
 * @create 2018-07-03 11:17 PM
 **/
@Service
public class ProducerSync2 {
    @Autowired
    private JmsTemplate jmsTemplate;

    @Value("response.queue")
    private String responseDestinationName;

    public void sendMsg(String destinationName, final String message) {
        System.out.println("---------------生产者发送消息-----------------");
        System.out.println("---------------生产者发了一个消息：" + message);
        Destination responseDestination = new ActiveMQQueue(responseDestinationName);
        Destination destination = new ActiveMQQueue(destinationName);

        jmsTemplate.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage(message);
                textMessage.setJMSReplyTo(responseDestination);
                return textMessage;
            }
        });
    }
}
